import { ref, computed } from 'vue'
import type { UserProfile, FriendRequest } from '../types/social'

export function useFriendSystem() {
  const friends = ref<UserProfile[]>([])
  const friendRequests = ref<FriendRequest[]>([])
  const onlineFriends = computed(() => friends.value.filter(f => f.status === 'online'))
  
  const sendFriendRequest = async (toUserId: string, message?: string) => {
    try {
      const response = await fetch('/api/friends/request', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          toUserId,
          message
        })
      })
      
      if (!response.ok) {
        throw new Error('Failed to send friend request')
      }
      
      const request = await response.json()
      friendRequests.value.push(request)
      return true
    } catch (error) {
      console.error('Error sending friend request:', error)
      return false
    }
  }

  const acceptFriendRequest = async (requestId: string) => {
    try {
      const response = await fetch(`/api/friends/request/${requestId}/accept`, {
        method: 'POST'
      })
      
      if (!response.ok) {
        throw new Error('Failed to accept friend request')
      }
      
      const newFriend = await response.json()
      friends.value.push(newFriend)
      
      // 从请求列表中移除
      friendRequests.value = friendRequests.value.filter(r => r.id !== requestId)
      return true
    } catch (error) {
      console.error('Error accepting friend request:', error)
      return false
    }
  }

  const rejectFriendRequest = async (requestId: string) => {
    try {
      const response = await fetch(`/api/friends/request/${requestId}/reject`, {
        method: 'POST'
      })
      
      if (!response.ok) {
        throw new Error('Failed to reject friend request')
      }
      
      // 从请求列表中移除
      friendRequests.value = friendRequests.value.filter(r => r.id !== requestId)
      return true
    } catch (error) {
      console.error('Error rejecting friend request:', error)
      return false
    }
  }

  const removeFriend = async (friendId: string) => {
    try {
      const response = await fetch(`/api/friends/${friendId}`, {
        method: 'DELETE'
      })
      
      if (!response.ok) {
        throw new Error('Failed to remove friend')
      }
      
      friends.value = friends.value.filter(f => f.id !== friendId)
      return true
    } catch (error) {
      console.error('Error removing friend:', error)
      return false
    }
  }

  const loadFriends = async () => {
    try {
      const response = await fetch('/api/friends')
      if (!response.ok) {
        throw new Error('Failed to load friends')
      }
      
      friends.value = await response.json()
    } catch (error) {
      console.error('Error loading friends:', error)
    }
  }

  const loadFriendRequests = async () => {
    try {
      const response = await fetch('/api/friends/requests')
      if (!response.ok) {
        throw new Error('Failed to load friend requests')
      }
      
      friendRequests.value = await response.json()
    } catch (error) {
      console.error('Error loading friend requests:', error)
    }
  }

  return {
    friends,
    onlineFriends,
    friendRequests,
    sendFriendRequest,
    acceptFriendRequest,
    rejectFriendRequest,
    removeFriend,
    loadFriends,
    loadFriendRequests
  }
} 